using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace MicroMsg.UI.UserContrl
{
	public class ChatCardItem : BaseChatElement
	{
		private const string TAG = "ChatCardItem";
		private Button ContentButton;
		private MenuItem ContentDelMenuItem;
		private MenuItem ContentForwardMenuItem;
		private CardMsgInfo mFmsgInfo;
		public static DependencyProperty FlagImgSourceProperty = DependencyProperty.Register("FlagImgSource", typeof(ImageSource), typeof(ChatCardItem), new PropertyMetadata(new ImageSourceConverter().ConvertFromString("/Resource/Img/Chat/flagQQ.png") as ImageSource, new PropertyChangedCallback(ChatCardItem.OnFlagImgSourceChanged)));
		public static DependencyProperty HeadImgSourceProperty = DependencyProperty.Register("HeadImgSource", typeof(ImageSource), typeof(ChatCardItem), new PropertyMetadata(new ImageSourceConverter().ConvertFromString("/Resource/Img/CommonUse/DefaultHead.png") as ImageSource, new PropertyChangedCallback(ChatCardItem.OnHeadImgSourceChanged)));
		public static DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(ChatCardItem), new PropertyMetadata(strings.Con_FMsg_TxtName, new PropertyChangedCallback(ChatCardItem.OnTitleChanged)));
		public static DependencyProperty NickNameProperty = DependencyProperty.Register("NickName", typeof(string), typeof(ChatCardItem), new PropertyMetadata("用户昵称", new PropertyChangedCallback(ChatCardItem.OnNickNameChanged)));
		public static DependencyProperty UserInfoProperty = DependencyProperty.Register("UserInfo", typeof(string), typeof(ChatCardItem), new PropertyMetadata("微信号:XXX", new PropertyChangedCallback(ChatCardItem.OnUserInfoChanged)));
		private bool isHeadInit;
		private EventWatcher mWatcherHeadImgAdd;
		public ImageSource FlagImgSource
		{
			get
			{
				return (ImageSource)base.GetValue(ChatCardItem.FlagImgSourceProperty);
			}
			set
			{
				base.SetValue(ChatCardItem.FlagImgSourceProperty, value);
			}
		}
		public ImageSource HeadImgSource
		{
			get
			{
				return (ImageSource)base.GetValue(ChatCardItem.HeadImgSourceProperty);
			}
			set
			{
				base.SetValue(ChatCardItem.HeadImgSourceProperty, value);
			}
		}
		public string Title
		{
			get
			{
				return (string)base.GetValue(ChatCardItem.TitleProperty);
			}
			set
			{
				base.SetValue(ChatCardItem.TitleProperty, value);
			}
		}
		public string NickName
		{
			get
			{
				return (string)base.GetValue(ChatCardItem.NickNameProperty);
			}
			set
			{
				base.SetValue(ChatCardItem.NickNameProperty, value);
			}
		}
		public string UserInfo
		{
			get
			{
				return (string)base.GetValue(ChatCardItem.UserInfoProperty);
			}
			set
			{
				base.SetValue(ChatCardItem.UserInfoProperty, value);
			}
		}
		public ChatCardItem()
		{
			base.add_Loaded(new RoutedEventHandler(this.ChatCardItem_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.ChatCardItem_Unloaded));
		}
		private void ChatCardItem_Unloaded(object sender, RoutedEventArgs e)
		{
			this.GetHeaderUnit();
		}
		private void ChatCardItem_Loaded(object sender, RoutedEventArgs e)
		{
			this.GetHeaderInit();
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this.ContentButton = (base.GetTemplateChild("ContentButton") as Button);
			this.ContentDelMenuItem = (base.GetTemplateChild("DeleteMsgMenu") as MenuItem);
			this.ContentForwardMenuItem = (base.GetTemplateChild("ForwardMsgMenu") as MenuItem);
			this.ContentButton.SetValue(MMTiltEffect.IsTiltEnabledProperty, true);
			this.ContentButton.add_Click(new RoutedEventHandler(this.ContentButton_Click));
			this.ContentDelMenuItem.Click += new RoutedEventHandler(this.ContentMenuItem_Click);
			this.ContentForwardMenuItem.Click += new RoutedEventHandler(this.ContentForwardMenuItem_Click);
		}
		private void ContentButton_Click(object sender, RoutedEventArgs e)
		{
			if (this.mclickEvent != null)
			{
				this.mclickEvent(this.chatMsg_0, this);
			}
		}
		private void ContentMenuItem_Click(object sender, RoutedEventArgs e)
		{
			if (this.chatMsg_0 == null)
			{
				return;
			}
			if (this.chatMsg_0 is ChatTimeMsg)
			{
				return;
			}
			ChatMsgMgr.deleteTalkMsg(this.chatMsg_0);
		}
		private void ContentForwardMenuItem_Click(object sender, RoutedEventArgs e)
		{
			if (this.chatMsg_0 == null)
			{
				return;
			}
			if (this.mFmsgInfo == null)
			{
				return;
			}
			Contact contact = StorageMgr.contact.method_0(this.mFmsgInfo.UserName);
			if (contact == null)
			{
				return;
			}
			ForwardMsgHelper.ToForwardMsg(contact, ForwardKind.Kind_ShareCard, null);
		}
		public void ItemClickHandle(ChatMsg chatMsg_1)
		{
			if (chatMsg_1 == null)
			{
				return;
			}
			if (this.mFmsgInfo == null)
			{
				return;
			}
			if (MainPage.mainPageInstance == null)
			{
				return;
			}
			ContactDetailViewArgs conDetailArgFromShareCardInfo = ContactDetailViewForAll.getConDetailArgFromShareCardInfo(this.mFmsgInfo);
			ContactDetailViewForAll.GotoProfile(conDetailArgFromShareCardInfo, false);
		}
		public override void SetChatMsg(ChatMsg Msg)
		{
			base.SetChatMsg(Msg);
			this.RefreshItem(Msg);
		}
		private void RefreshItem(ChatMsg chatMsg_1)
		{
			if (chatMsg_1 == null)
			{
				return;
			}
			string text = "";
			string text2 = "";
			if (!ChatMsgMgr.getMsgUser(chatMsg_1, ref text, ref text2))
			{
				Log.smethod_1("ChatCardItem", "getMsgUser error:" + text2);
				return;
			}
			this.mFmsgInfo = CardParse.ParseCardMsgInfoXML(text2);
			if (this.mFmsgInfo == null)
			{
				base.set_Height(0.0);
				base.set_Visibility(1);
				return;
			}
			this.Title = this.mFmsgInfo.Title;
			this.NickName = this.mFmsgInfo.NickName;
			this.UserInfo = this.mFmsgInfo.UserInfo;
			this.HeadImgSource = HeadImgMgr.getHeadImage(this.mFmsgInfo.UserName, true);
			this.FlagImgSource = this.mFmsgInfo.FlagImg;
		}
		private static void OnFlagImgSourceChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
		private static void OnHeadImgSourceChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
		private static void OnTitleChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
		private static void OnNickNameChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
		private static void OnUserInfoChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
		private void GetHeaderInit()
		{
			if (this.isHeadInit)
			{
				return;
			}
			this.mWatcherHeadImgAdd = new EventWatcher(this, null, new EventHandlerDelegate(this.onEventHandlerHeadImgAdd));
			EventCenter.registerEventWatcher(EventConst.ON_MGR_HEAD_IMG_READY, this.mWatcherHeadImgAdd);
			Log.smethod_5("ChatCardItem", "registerEventWatcher EventConst.ON_MGR_HEAD_IMG_READY");
			this.isHeadInit = true;
		}
		private void GetHeaderUnit()
		{
			if (!this.isHeadInit)
			{
				return;
			}
			EventCenter.removeEventWatcher(EventConst.ON_MGR_HEAD_IMG_READY, this.mWatcherHeadImgAdd);
			Log.smethod_5("ChatCardItem", "removeEventWatcher EventConst.ON_MGR_HEAD_IMG_READY");
			this.isHeadInit = false;
		}
		private void onEventHandlerHeadImgAdd(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			List<string> listObject = evtArgs.getListObject<string>();
			if (listObject == null)
			{
				return;
			}
			if (this.mFmsgInfo == null)
			{
				return;
			}
			if (string.IsNullOrEmpty(this.mFmsgInfo.UserName))
			{
				return;
			}
			using (List<string>.Enumerator enumerator = listObject.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					string current = enumerator.get_Current();
					if (current != null && current.GetType() == typeof(string) && current == this.mFmsgInfo.UserName)
					{
						this.HeadImgSource = HeadImgMgr.getHeadImage(this.mFmsgInfo.UserName, true);
						break;
					}
				}
			}
		}
	}
}
